package views.games;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import models.games.PesciDisorientati.CardinalPoints;

import org.jdesktop.animation.timing.Animator;
import org.jdesktop.animation.timing.Animator.RepeatBehavior;
import org.jdesktop.animation.timing.interpolation.PropertySetter;

import views.util.GameColors;

public class PesciDisorientatiView extends GameView {

	private Animator animator;
	// private ScreenTransition transition = new ScreenTransition(this, this,
	// animator);
	// private int nextScreen = 0;
	private ImageIcon[] yellowFishesImages = new ImageIcon[4];
	private ImageIcon[] redFishesImages = new ImageIcon[4];
	private final int NORTH = 0, EAST = 1, SOUTH = 2, WEST = 3;
	private JButton[] arrayButtons = new JButton[4];
	// private int prevW, prevH;
	private JPanel fishesBank;
	private JLabel[] fishesActualImages;
	private final int DURATION_MILLIS = 2000;
	private JPanel main;
	private JPanel bankContainer;
	private Point start;
	private Point end;
	private ActionListener currentActionListener;
	private KeyListener currentKeyListener;

	public PesciDisorientatiView() {
		super(GameColors.ARANCIONE, GameColors.ARANCIONE_CHIARO, "PREMI INVIO",
				"PUNTEGGIO",
				("<html>GIALLO = ORIENTAMENTO<BR>ROSSO = DIREZIONE</html>"));
		this.other.setFont(new Font("Segoe UI", Font.BOLD, 15));
		main = new JPanel();
		main.setLayout(new BorderLayout());
		add(main);
		yellowFishesImages[0] = new ImageIcon(
				"resources/pesciDisorientati/fishNorth.png");
		yellowFishesImages[1] = new ImageIcon(
				"resources\\pesciDisorientati\\fishEast.png");
		yellowFishesImages[2] = new ImageIcon(
				"resources\\pesciDisorientati\\fishSouth.png");
		yellowFishesImages[3] = new ImageIcon(
				"resources\\pesciDisorientati\\fishWest.png");
		redFishesImages[0] = new ImageIcon(
				"resources\\pesciDisorientati\\fishNorthRed.png");
		redFishesImages[1] = new ImageIcon(
				"resources\\pesciDisorientati\\fishEastRed.png");
		redFishesImages[2] = new ImageIcon(
				"resources\\pesciDisorientati\\fishSouthRed.png");
		redFishesImages[3] = new ImageIcon(
				"resources\\pesciDisorientati\\fishWestRed.png");

		fishesBank = new JPanel();
		fishesBank.setLayout(new GridLayout(3, 3));

		fishesActualImages = new JLabel[9];
		// /////
		for (int i = 0; i < fishesActualImages.length; i++)
			fishesActualImages[i] = new JLabel(yellowFishesImages[3]);
		// ////
		for (int i = 0; i < fishesActualImages.length; i++)
			fishesBank.add(fishesActualImages[i]);
		fishesBank.setBackground(GameColors.ARANCIONE);
		animator = new Animator(DURATION_MILLIS);// DEFAULT
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(1, 4));
		arrayButtons[0] = new JButton("SU");
		arrayButtons[1] = new JButton("GIU'");
		arrayButtons[2] = new JButton("DESTRA");
		arrayButtons[3] = new JButton("SINISTRA");
		arrayButtons[0].setName("SU");
		arrayButtons[1].setName("GIU'");
		arrayButtons[2].setName("DESTRA");
		arrayButtons[3].setName("SINISTRA");
		for (int i = 0; i < arrayButtons.length; i++)
			buttonPanel.add(arrayButtons[i]);
		main.add(buttonPanel, BorderLayout.SOUTH);
		bankContainer = new JPanel();
		bankContainer.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 1;
		gbc.gridy = 1;
		bankContainer.add(fishesBank);
		bankContainer.setBackground(GameColors.ARANCIONE);
		main.add(bankContainer);
	}

	public void checkFocusMain() {
		main.requestFocus();
	}

	// public void startTimer(){
	// timer.start();
	// }

	public void update(int direction, CardinalPoints[] fishesOrientation,
			boolean isDirection) {

		switch (direction) {
		case NORTH:
			start = new Point(bankContainer.getWidth() / 2
					- fishesBank.getWidth() / 2, bankContainer.getHeight());
			end = new Point(bankContainer.getWidth() / 2
					- fishesBank.getWidth() / 2, -fishesBank.getWidth());
			break;
		case EAST:
			start = new Point(0 - fishesBank.getWidth(),
					bankContainer.getHeight() / 2 - fishesBank.getHeight() / 2);
			end = new Point(bankContainer.getWidth() + fishesBank.getWidth(),
					bankContainer.getHeight() / 2 - fishesBank.getHeight() / 2);
			break;
		case SOUTH:
			start = new Point(bankContainer.getWidth() / 2
					- fishesBank.getWidth() / 2, -fishesBank.getWidth());
			end = new Point(bankContainer.getWidth() / 2
					- fishesBank.getWidth() / 2, bankContainer.getHeight()
					+ fishesBank.getWidth() / 2);
			break;
		case WEST:
			start = new Point(bankContainer.getWidth() + fishesBank.getWidth(),
					bankContainer.getHeight() / 2 - fishesBank.getHeight() / 2);
			end = new Point(0 - fishesBank.getWidth(),
					bankContainer.getHeight() / 2 - fishesBank.getHeight() / 2);
			break;
		}
		animator.stop();

		animator = PropertySetter.createAnimator(4500, this, "fishesBank",
				start, end);
		animator.setRepeatBehavior(RepeatBehavior.LOOP);
		animator.setRepeatCount(Animator.INFINITE);
		animator.start();
		setFishesImages(fishesOrientation, isDirection);
	}

	public JPanel getFishesBank() {
		return fishesBank;
	}

	public void setFishesBank(Point newEnd) {
		System.out.println(newEnd);
		fishesBank.setLocation(newEnd);
	}

	/**
	 * @param fishesOrientation
	 *            di length 9
	 */
	private void setFishesImages(CardinalPoints[] fishesOrientation,
			boolean isDirection) {
		if (isDirection)
			for (int i = 0; i < fishesOrientation.length; i++) {
				fishesActualImages[i]
						.setIcon(redFishesImages[fishesOrientation[i].ordinal()]);
			}
		else
			for (int i = 0; i < fishesOrientation.length; i++) {
				fishesActualImages[i]
						.setIcon(yellowFishesImages[fishesOrientation[i]
								.ordinal()]);
			}

	}

	public void addActionListenerToTheGame(ActionListener a) {
		currentActionListener = a;
		arrayButtons[0].addActionListener(a);
		arrayButtons[1].addActionListener(a);
		arrayButtons[2].addActionListener(a);
		arrayButtons[3].addActionListener(a);
	}

	public void addKeyListenerToTheGame(KeyListener k) {
		currentKeyListener = k;
		arrayButtons[0].addKeyListener(k);
		arrayButtons[1].addKeyListener(k);
		arrayButtons[2].addKeyListener(k);
		arrayButtons[3].addKeyListener(k);
		main.addKeyListener(k);
	}

	@Override
	public void gameEnded() {
		other.setVisible(false);
		gameClock.setVisible(false);
		animator.stop();
		main.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		main.removeAll();
		main.setBackground(GameColors.ARANCIONE);
		for (JLabel l : fishesActualImages) {
			l.setIcon(null);
		}
		JLabel end = newLabel("Partita Conclusa!", GameColors.ARANCIONE_CHIARO);
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.gridwidth = 4;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets(0, 0, 0, 0);
		main.add(end, gbc);
		gbc.gridy = 1;
		main.add(score, gbc);
		JButton restart = newButton("Ricomincia");
		JButton menu = newButton("Menu Giochi");
		gbc.gridy = 2;
		gbc.gridwidth = 2;
		gbc.gridx = 0;
		main.add(restart, gbc);
		gbc.gridx = 2;
		main.add(menu, gbc);
		restart.addActionListener(currentActionListener);
		menu.addActionListener(currentActionListener);
		main.removeKeyListener(currentKeyListener);
		for (JButton b : arrayButtons) {
			b.removeKeyListener(currentKeyListener);
			b.removeActionListener(currentActionListener);
		}
		updateUI();

	}

	// @Override
	// public void actionPerformed(ActionEvent arg0) {
	// timeMax--;
	// gameClock.setText("0:" + timeMax);
	// if (timeMax == 0){
	// timer.stop();
	// gameEnded();
	// }
	//
	// }

}
